<?php

namespace app\index\controller;

// use app\common\controller\Frontend;
use app\index\controller\User;
use think\facade\Config;
use think\facade\Cookie;
use think\facade\Hook;
use think\facade\Session;
use think\facade\Validate;
use think\facade\Cache;

/**
 * 流量情况
 */
class Home extends User


{    

    public function initialize()
    {
        parent::initialize();
        $this->token = session("login_token");       
        if(!$this->token){
            $this->redirect("index/user/login");
        }
    }
    

    public function index()
    {

        //下面的状态   0：隐藏   1：展示

        //验证用户,获取用户的权限菜单    
        $user_power = session('user_power');
        $row = [];
        foreach ($user_power as $value) {
            array_push($row,$value['name']);
        }
        if(in_array('index/log/index',$row)){
            $state = 1;
        }else{
            $state = 0;
        }

        //获取用户的权限,判断是否需要展示数据
        $rule = session('user_rule');
        $rows = [];
        foreach($rule['data'] as $v){
            array_push($rows,$v['name']);
        }
        //是否有top10的权限
        if(in_array('api/application/topn',$rows)){
            $state_top = 1;
        }else{
            $state_top = 0;
        }

        //新版上下行流量趋势
        if(in_array('api/es/flow',$rows)){
            $news = 1;
        }else{
            $news = 0;
        }

        $this->assign('state',$state);
        $this->assign('state_top',$state_top);
        $this->assign('news',$news);
        return view();
    }

    //综合质量
    public function comprehensive()
    {
        $datas = array(
            'token'         =>  session('login_token'),
            'terminal'      =>  'web'
        );
        $data_string = json_encode($datas);
        $url = request()->domain() . '/api/user/snap';
        ##  调内部接口
        $res = $this->curl($url,$data_string);    
        return $res;
    }
    


    //应用排行top10
    public function info()
    {
        $data = array(
            'token' => session('login_token'),
            'terminal' => 'web'
        );
        $data_string = json_encode($data);
        $url = request()->domain() . '/api/application/topn';
        ##  调内部接口
        $res = $this->curl($url,$data_string);        
        return $res;
    }

    //新版天路云获取24小时流量趋势
    public function new_flow()
    {
        //获取用户的ip
        $row = array(
            'token' => session('login_token'),
            "ip"=> session('ip'),
            "terminal"=>"web"
        );
        $data_string = json_encode($row);
        $url = request()->domain() . '/api/es/flow';
        $res = $this->curl($url,$data_string);
        return $res;
    }


    /**
     * 透传内部接口
     *
     * @param      <type>  $url    The url
     * @param      <type>  $data   The data
     *
     * @return     <type>  ( description_of_the_return_value )
     */
    public function curl($url,$data_string){                
        $curlopt = array(
            CURLOPT_HTTPHEADER=>array ('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen ( $data_string ) ),
            CURLOPT_TIMEOUT=>Config::get("site.api_timeout"),
        );        
        //发请求拿数据
        $data = curl_post($url, $data_string, $curlopt);        
        return $data;
    } 
}
